<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnbd.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);

if(isset($_POST['txt_name'])){
	if($catId = $MAIN->categories->setCategory($_POST['txt_category_id'],$_POST)){
		if($_REQUEST['view']=="add"){
		?>
		<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=categories&view=edit&id=<?php echo($catId)?>';}).delay(3000);</script>
		<?php
		}else{
		?>
		<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=categories&view=all';}).delay(3000);</script>
		<?php
		}
	};
	exit();
}
if(isset($_REQUEST['del'])){
	if($MAIN->categories->delCategory($_REQUEST['del'])){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=categories&view=all';}).delay(3000);</script>
	<?php	
	};
	exit();
}
?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){
	default:
	case 'all':	
	?>
	<h1>Alla mappar i sistemet</h1>
	<ul>
	<?php
	$parents = $MAIN->categories->getParentCategories();	
	foreach($parents as $parent){
		$subCats =$MAIN->categories->getCategoriesByParent($parent['id']);
		?>
		<li>
			<?php echo($parent['name']);?> <?php if($MAIN->currentUser['grouplevel']<50){?>(<i style="font-size:10px;"><?php if(count($subCats)==0){?><a href="javascript:void(0);" onclick="delCategory(<?php echo($parent['id']);?>)">Ta Bort</a> | <?php }?><a href="?cat=categories&amp;view=edit&amp;id=<?php echo($parent['id']);?>">Ändra</a></i>)<?php }?>
		<?php		
		if(count($subCats)>0){
			?>
			<ul>
			<?php
			foreach($subCats as $category){
				?>
				<li><?php echo($category['name']);?> <?php if($MAIN->currentUser['grouplevel']<50){?>(<i style="font-size:10px;"><a href="javascript:void(0);" onclick="delCategory(<?php echo($category['id']);?>)">Ta Bort</a> | <a href="?cat=categories&amp;view=edit&amp;id=<?php echo($category['id']);?>">Ändra</a></i>)<?php }?></li>
				<?php
			}
			?>
			</ul>
			<?php
		}
		?>		
		</li>
		<?php	
	}
	?>
	</ul>	
	<script type="text/javascript">
		var delCategory = function(id){
			var msg='Bekräfta borttagning';
			if(confirm(msg)){
			ajaxGet('categories.php','del='+id);
			}
		}
	</script>
	<?php
	break;
	case 'edit':
	case 'add':
		$category=$MAIN->categories->getCategory(@$_REQUEST['id']);
	?>
	<h1>Skapa mapp</h1>
	<form name="frm_category" id="frm_category" action="categories.php" method="post" onsubmit="return false;">
		<input type="hidden" name="txt_category_id" value="<?php echo($category['id']);?>"/>
		<input type="hidden" name="ddl_rights" value="0"/>
		<input type="hidden" name="view" value="<?php echo($_REQUEST['view']);?>"/>
	<table cellpadding="0" cellspacing="0">
		<tr>
			<td>Mapp namn</td>
			<td><input type="text" name="txt_name" value="<?php echo($category['name']);?>"/></td>
		</tr>
		<tr>
			<td>Mapp visningnamn</td>
			<td><input type="text" name="txt_display_name" value="<?php echo($category['display_name']);?>"/></td>
		</tr>
		<tr>
			<td>Undermapp</td>
			<td>
				<?php
				$hasSubcategories = $MAIN->categories->getCategoriesByParent($category['id']);
				$hasSubcategories = count($hasSubcategories)==0;
				if($hasSubcategories){
				?>
				<select name="ddl_parent_cat" id="ddl_parent_cat">
					<option value="0">-huvudkategory-</option>
				<?php 
				$parents = $MAIN->categories->getParentCategories();				
				foreach($parents as $parent){
				?>
				<option <?php if($parent['id']==$category['parent_id']){?>selected="selected"<?php }?> value="<?php echo($parent['id']);?>"><?php echo($parent['name']);?></option>
				<?php
				}
				?>
				</select>
				<?php
				}else{
				?>
				<input type="hidden" name="ddl_parent_cat" value="0"/>
				<select name="x" id="x" disabled="disabled">
					<option value="0">-huvudkategory-</option>
				</select>
				<?php
				}
				?>
			</td>
		</tr>
		<tr>
			<td>Beskrivning</td>
			<td><textarea name="txt_desc" rows="" cols=""><?php echo($category['desc']);?></textarea></td>
		</tr>		
		<tr>
			<td colspan="2" style="text-align:right;">
				<input type="submit" name="btn_submit" value="Spara" onclick="sendForm('frm_category');"/>
			</td>
		</tr>
	</table>
	</form>
	<?php
	break;
}
?>
</div>